package com.ruoyi.commentUpvote.controller;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.commentUpvote.domain.CommentUpvote;
import com.ruoyi.commentUpvote.service.ICommentUpvoteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评论点赞Controller
 * 
 * @author ruoyi
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/commentUpvote/commentUpvote")
public class CommentUpvoteController extends BaseController
{
    @Autowired
    private ICommentUpvoteService commentUpvoteService;

    /**
     * 查询评论点赞列表
     */
    @PreAuthorize("@ss.hasPermi('commentUpvote:commentUpvote:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommentUpvote commentUpvote)
    {
        startPage();
        List<CommentUpvote> list = commentUpvoteService.selectCommentUpvoteList(commentUpvote);
        return getDataTable(list);
    }

    /**
     * 导出评论点赞列表
     */
    @PreAuthorize("@ss.hasPermi('commentUpvote:commentUpvote:export')")
    @Log(title = "评论点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommentUpvote commentUpvote)
    {
        List<CommentUpvote> list = commentUpvoteService.selectCommentUpvoteList(commentUpvote);
        ExcelUtil<CommentUpvote> util = new ExcelUtil<CommentUpvote>(CommentUpvote.class);
        util.exportExcel(response, list, "评论点赞数据");
    }

    /**
     * 获取评论点赞详细信息
     */
    //@PreAuthorize("@ss.hasPermi('commentUpvote:commentUpvote:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(commentUpvoteService.selectCommentUpvoteById(id));
    }

    /**
     * 新增评论点赞
     */
    //@PreAuthorize("@ss.hasPermi('commentUpvote:commentUpvote:add')")
    @Log(title = "评论点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommentUpvote commentUpvote)
    {
        return toAjax(commentUpvoteService.insertCommentUpvote(commentUpvote));
    }

    /**
     * 修改评论点赞
     */
   // @PreAuthorize("@ss.hasPermi('commentUpvote:commentUpvote:edit')")
    @Log(title = "评论点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommentUpvote commentUpvote)
    {
        return toAjax(commentUpvoteService.updateCommentUpvote(commentUpvote));
    }

//    wx修改用户点赞
    @PostMapping("/update")
    public AjaxResult editCommentUpvote(@RequestBody String commentUpvoteList){

        System.out.println("========="+commentUpvoteList);
        JSONObject jsonObject = JSON.parseObject(commentUpvoteList);
        JSONArray commentUpvoteList1 = jsonObject.getJSONArray("commentUpvoteList");
        List<CommentUpvote> list = new ArrayList<>();
        for (Object o : commentUpvoteList1) {
            list.add(JSON.parseObject(o.toString(), CommentUpvote.class));
        }
        return toAjax(commentUpvoteService.updateCommentUpvote(list));
    }

    /**
     * 删除评论点赞
     */
    @PreAuthorize("@ss.hasPermi('commentUpvote:commentUpvote:remove')")
    @Log(title = "评论点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(commentUpvoteService.deleteCommentUpvoteByIds(ids));
    }
}
